package com.link2room.soundwave.facade.impl;

import com.greencloud.api.entity.Hotel;
import com.greencloud.api.request.HotelRequest;
import com.greencloud.api.response.HotelRespose;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.dto.param.manager.PmsHotelManagerParamDto;
import com.link2room.soundwave.entity.L2RIpms2Group;
import com.link2room.soundwave.entity.L2RIpms2Hotel;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.dto.result.base.PmsHotelDto;
import com.link2room.soundwave.util.GcPmsUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by VegetaKo on 2016/12/9.
 */
@Component("gcPmsHotelHandlerFacadeService")
public class GcPmsHotelHandlerFacadeServiceImpl extends AbstractPmsHotelHandlerFacadeServiceImpl {
    @Resource
    private ICommonService commonService;

    @Override
    PmsHotelDto findPmsHotel(BaseL2rParamDto baseL2rParamDto) throws Exception {
        L2RIpms2Hotel queryHotel=new L2RIpms2Hotel();
        queryHotel.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
        queryHotel.setLhotelCode(baseL2rParamDto.getLhotelCode());
        L2RIpms2Hotel l2RIpms2Hotel=commonService.findOneEQ(L2RIpms2Hotel.class,queryHotel);
        PmsHotelDto pmsHotelDto=null;
        if(l2RIpms2Hotel!=null){
            pmsHotelDto=new PmsHotelDto();
            pmsHotelDto.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
            pmsHotelDto.setLhotelCode(baseL2rParamDto.getLhotelCode());
            pmsHotelDto.setHotelGroupCode(l2RIpms2Hotel.getHotelGroupId().toString());
            pmsHotelDto.setHotelCode(l2RIpms2Hotel.getHotelId().toString());
            pmsHotelDto.setHotelName(l2RIpms2Hotel.getHotelName());
            pmsHotelDto.setId(l2RIpms2Hotel.getId());
        }
        return pmsHotelDto;
    }

    @Override
    QueryResult<PmsHotelDto> listPmsHotelNotMapping(BaseL2rParamDto baseL2rParamDto) throws Exception {
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        if(l2RIpms2Group!=null){
            QueryResult<PmsHotelDto> pmsHotelDtoQueryResult=new QueryResult<>();
            List<PmsHotelDto> pmsHotelList=new ArrayList<>();

            L2RIpms2Hotel queryHotel=new L2RIpms2Hotel();
            queryHotel.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
            QueryResult<L2RIpms2Hotel> l2RIpms2HotelResult = commonService.findAllEQ(L2RIpms2Hotel.class, queryHotel);
            List<L2RIpms2Hotel> l2RIpms2HotelList = l2RIpms2HotelResult.getResultlist();

            HotelRequest request=HotelRequest.newInstance();
            request.setHotelGroupCode(l2RIpms2Group.getPmsHotelGroupCode());
            HotelRespose resp = GcPmsUtil.getGcClient(baseL2rParamDto.getLhotelGroupCode()).execute(request);
            if(resp.getResultCode()<0){
                throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_HOTEL_ERROR,resp.getResultMsg());
            }else {
                List<Hotel> pmsHotels = resp.getResult();
                Iterator<Hotel> pmsHotelIterator = pmsHotels.iterator();
                while (pmsHotelIterator.hasNext()){
                    Hotel pmsHotel=pmsHotelIterator.next();
                    if(l2RIpms2HotelList!=null&&l2RIpms2HotelList.size()>0){
                        for(L2RIpms2Hotel l2RIpms2Hotel:l2RIpms2HotelList){
                            if(pmsHotel.getId().equals(l2RIpms2Hotel.getHotelId())){
                                pmsHotelIterator.remove();
                            }
                        }
                    }
                }
                PmsHotelDto pmsHotelDto;
                for (Hotel pmsHotel:pmsHotels){
                    pmsHotelDto=new PmsHotelDto();
                    pmsHotelDto.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
                    pmsHotelDto.setLhotelCode(baseL2rParamDto.getLhotelCode());
                    pmsHotelDto.setHotelGroupCode(pmsHotel.getHotelGroupId().toString());
                    pmsHotelDto.setHotelCode(pmsHotel.getId().toString());
                    pmsHotelDto.setHotelName(pmsHotel.getDescript());
                    pmsHotelList.add(pmsHotelDto);
                }
            }
            pmsHotelDtoQueryResult.setCurrentPage(1);
            pmsHotelDtoQueryResult.setTotalrecord(pmsHotelList.size());
            pmsHotelDtoQueryResult.setResultlist(pmsHotelList);
            return pmsHotelDtoQueryResult;
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_MAPPINGGROUP_NOT_EXIT);
        }
    }

    @Override
    void savePmsHotel(PmsHotelManagerParamDto pmsHotelManagerParamDto) throws Exception {
        L2RIpms2Hotel l2RIpms2Hotel=ClassConverUtil.copyProperties(pmsHotelManagerParamDto.getPmsHotelDto(),L2RIpms2Hotel.class);
        l2RIpms2Hotel.setHotelGroupId(Long.parseLong(pmsHotelManagerParamDto.getPmsHotelDto().getHotelGroupCode()));
        l2RIpms2Hotel.setHotelId(Long.parseLong(pmsHotelManagerParamDto.getPmsHotelDto().getHotelCode()));
        commonService.save(l2RIpms2Hotel);
    }

    @Override
    void delPmsHotel(BaseL2rParamDto baseL2rParamDto) throws Exception {
        commonService.delete(L2RIpms2Hotel.class,baseL2rParamDto.getId());
    }
}
